package com.seeyon.apps.kk.form.workflowevent;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;

/**
 * 邀请函触发测试
 * 
 * @author
 *
 */
public class KkYaoQingHanChuFaExtendPlug extends KkBaseExtendPlug {
	private static final Log log = LogFactory.getLog(KkYaoQingHanChuFaExtendPlug.class);

	@Override
	public String getId() {
		return "kkYaoQingHanChuFaExtendPlug";
	}

	@Override
	public String getLabel() {
		return "补充邀请函节点触发";
	}

	@Override
	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}

	public void onFinishWorkitem(com.seeyon.ctp.workflow.event.WorkflowEventData data) {

		Map<String, Object> formMap = data.getBusinessData();
		FormDataMasterBean fdmb = (FormDataMasterBean) formMap.get("formDataBean");
		ColSummary colSummary=(ColSummary)data.getSummaryObj();

		 
		
		FormTableBean ftb = fdmb.getFormTable();
		String tablekey = ftb.getTableName();
		String pathForm = "YQH" + File.separator + "YQH_XML.ftl";
		String path = "YQH" + File.separator + tablekey + "_sql.ftl";
		log.info("kk:pathForm"+pathForm);
		log.info("kk:pathForm"+path);
		if(colSummary!=null) {
			log.info("kk:colSummarygetFormRecordid"+colSummary.getFormRecordid());
			log.info("kk:colSummary"+colSummary.getId());
		}

		 	Integer field0268=MapUtils.getInteger(formMap, "field0268"); // 退单给销售校验	
		 	Integer field0269=MapUtils.getInteger(formMap, "field0269");  //退单给发起人校验
		 	
		 	log.info("kk:field0268"+field0268);
		 	log.info("kk:field0269"+field0269);
		 	
		 	if(field0268>=1) {
		 		if(field0269==0) {
		 			
		 			
		 			String sql = this.freemarkFlowManager.fltTemplateIntoString(path, colSummary);
		 			log.info("kk:sql"+sql);
		 			log.info("kk:formMapid "+formMap.get("id"));
		 			log.info("kk:fdmb "+fdmb.getId());
		 			log.info("kk:SummaryId"+data.getSummaryId());
		 			
		 			List<Map<String, Object>> vl = this.kkJdbcTemplate.queryForList(sql);
		 			
		 			log.info("kk:vl"+vl.size());
		 			
		 			for (Iterator<Map<String, Object>> iterator = vl.iterator(); iterator.hasNext();) {
		 				Map<String, Object> vm = iterator.next();
		 				String xmlData = this.freemarkFlowManager.fltTemplateIntoString(pathForm, vm);
		 				String senderLoginName = MapUtils.getString(vm, "senderLoginName");
		 				log.info("kk:senderLoginName"+senderLoginName);
		 				log.info("kk:vm"+vm.toString());
		 				log.info("kk:xmlData"+xmlData);
		 				
		 				if (StringUtils.isNotBlank(xmlData)) {
		 					try {
		 						this.freemarkFlowManager.sendCollaboration(senderLoginName, "YQH_001", "", xmlData, null, null,
		 								null);
		 					} catch (Exception e) {
		 						// TODO Auto-generated catch block
		 						log.info("kk:flowerror"+e.getMessage());
		 						e.printStackTrace();
		 					}
		 				}

		 			}
		 			
		 			
		 		}
		 	}	

	}

}
